<?php

namespace app\index\controller;

class Vote extends Base
{
    //前置方法
    protected $beforeActionList = [
        'vote' => ['only' => 'detailed'],
        'right' => ['only' => 'index,detailed'],

    ];

    public function vote()
    {
        $id = $this->request->param('id');
        $list = db('vote_item')->where(array('vid' => $id))->order('rank desc,id desc')->select();
        $this->assign('list', $list);
    }


    /**
     * 保存调研结果
     */
    public function savevote()
    {
        $oo = $this->request->param();

        if (empty($oo['item'])) {
            return ajax_info('选择');

        }
        db('vote')->where('id', $this->request->param('id'))->setInc('count');

        foreach ($oo['item'] as $k => $v) {
            $value[] = $v;
        }
        $date = implode(',', $value);

        $record = array();
        $record['vid'] = $this->request->param('id');
        $record['item_id'] = $date;
        $record['touch_time'] = time();
        db('vote_record')->insert($record);
        db('vote_item')->where('id', 'in', $date)->setInc('vcount');

        return ajax_info('投票成功');


    }

}
